﻿#region File Description
///////////////////////////////////////////////////////////////////////////////
/// \file GOLRuleset.cs
/// \brief This file stores the GameOfLife's implementation of Ruleset
/// \author Bradley Mclain, Steven Duda
/// \date 16/5/2010
/// \remarks Copyright(c) 2010 LM01 Project Group (Animus Games - ViralFlux)
/// \remarks Some Rights Reserved.
/// Licenced under: Microsoft Reciprocal License (Ms-RL)
/// For More Information see: www.microsoft.com/opensource/licenses.mspx
///////////////////////////////////////////////////////////////////////////////
#endregion

#region Using Statements
    using System;
    using PropagationEngine;
#endregion

namespace GameOfLife
{
    /// <summary>
    /// \class GOLRuleset
    /// \brief This class provides GameOfLife's implemented Ruleset
    /// \author Bradley Mclain, Steven Duda
    /// \date 16/5/2010
    /// </summary>
    internal sealed class GOLRuleset : Ruleset
    {
        /// <summary>
        /// \brief Check if the given entity can transmit from source to destination node
        /// \param sourceNode Where the entity came from
        /// \param destinationNode Where the entity is going
        /// \param entity The Entity in question
        /// \return The amount of entity that spreads
        /// \author Bradley Mclain, Steven Duda
        /// \date 16/5/2010
        /// </summary>
		public override int CheckExternalSpread(Node sourceNode, Node destinationNode, Transmission transmission)
        {
            GOLNodeProperties prop = (GOLNodeProperties)sourceNode.Properties;

            //If alive allow to transmit
            return (prop.State == GOLNodeState.Alive ? 1 : 0);
        }

        /// <summary>
        /// \brief Check if the given entity can spread further within a node
        /// \param node The Node in question
        /// \return The amount of entity that spreads
        /// \author Bradley Mclain, Steven Duda
        /// \date 16/5/2010
        /// </summary>
		public override int CheckInternalSpread(Node node, Transmission transmission)
        {
            //Number of recieved transmissions
            Transmission rcv = node.TransmissionsList.Find(item => item.ContainedEntity.ID == transmission.ContainedEntity.ID);

            GOLNodeProperties prop = (GOLNodeProperties)node.Properties; //< Testing
            if ((rcv.Amount < 2 || rcv.Amount > 3) && (prop.State == GOLNodeState.Alive))
            {
                //Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
                //Any live cell with more than three live neighbours dies, as if by overcrowding.
                prop.State = GOLNodeState.Dead;
                #if DEBUG
                    Console.WriteLine("Killed node: " + node.ID + " amount: " + rcv.Amount); ///! <debug>
                #endif
            }
            else if (rcv.Amount == 3 && prop.State == GOLNodeState.Dead)
            {
                //Any dead cell with exactly three live neighbours becomes a live cell.
                prop.State = GOLNodeState.Alive;
                #if DEBUG
                    Console.WriteLine("Revived node: " + node.ID + " amount: " + rcv.Amount); ///! <debug>
                #endif
            }

            //Any live cell with two or three live neighbours lives on to the next generation.

            //Set the amount of recieved transmissions back to zero for next iteration
            rcv.Amount = 0;

            return 0;
        }

		public override void Cleanup(Node node)
        {
            //No Cleanup Required
        }

		public override void EntityInteraction(Node node)
        {
            //No Entity Interaction
        }


    }
}
